% Solve for the Ramsey problem with an affine tax
load sb; c = sb.c; save c c;

temp.tau = fminsearch(@(x) -RamseyAffineObjective(x,pr,w,pai,pw,G),0.5);
RamseyAffineObjective(temp.tau,pr,w,pai,pw,G);

%% Allocation
load tau
load c
load y
r_affine.tau     = tau;
r_affine.c       = c;
r_affine.y       = y;
r_affine.output  = sum(pai.*y);
r_affine.welfare = sum(pw.*pai.*Utility(c,y,w,pr));
r_affine.MTR     = 1-pr.Om./c.^(-pr.gam).*w.^-(1+pr.sig).*y.^pr.sig;
r_affine.ATR     = (y-c)./y;
r_affine.WelfareGain = 100*fzero(@(x) sum(pw.*pai.*Utility((1+x).*baseline.c,baseline.y,w,pr))-r_affine.welfare,.5);
r_affine.OutputLoss  = 100*(r_affine.output/baseline.output-1);
r_affine.EfAveMTR= sum(pai.*r_affine.MTR.*r_affine.y/r_affine.output);
[temp.y,temp.i]  = min(r_affine.y);
temp.c           = r_affine.c(temp.i);
r_affine.Tr2Y    = (temp.c-temp.y)./r_affine.output;
r_affine.TrG2Y   = (temp.c-temp.y+G)./r_affine.output;

save ./variables/r_affine r_affine
delete c.mat y.mat tau.mat